/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.ui;

import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author AJF
 */
public class GameScreen extends GameCanvas
implements Runnable{
    
    GameMidlet host;
    private final String GRASS_IMAGE = "/cl/resources/grass.png";
    private final String MR_SMITH_IMAGE = "/cl/resources/mrsmith.png";
    private final String BIRD_IMAGE = "/cl/resources/bird.png";
    private final String ROCK_IMAGE = "/cl/resources/rock.png";
    private final String EXPLOSION_IMAGE = "/cl/resources/explosion.png";
    private LayerManager layerManager;
    private int width = getWidth();
    private int height = getHeight();
    private TiledLayer grassBackground;
    Graphics g;
    
    //Sprite definition
    private Sprite mrSmith;
    private Sprite bird;
    private Sprite rock;
    private boolean birdHitByTone = false;
    private boolean gameIsOn = true;
    private int score;
    private int birdSpeed = 20;
    private boolean rockIsThrown = false;
    private int rockYSpeed;
    private int rockXSpeed;
    private boolean rockDirection;
    private final int gravity = 2;
    private final static boolean DIRECTION_LEFT = false;
    private final static boolean DIRECTION_RIGHT = true;
    private boolean direction;
    private Random random = new Random(System.currentTimeMillis());
    private Sprite explotion;
    
    private final int speed = 50;
    
    public GameScreen(GameMidlet host){
        super(false);
        this.host = host;
        setFullScreenMode(gameIsOn);
        gameIsOn = true;
        birdHitByTone = false;
        score = 0;
    }
    
    private Image loadImage(String path) {
        Image image = null;
        try{
            image = Image.createImage(path);
        }catch (Exception e) {            
        }
        return image;
    }
    
    //buat background
    /*
    private void createBackground(){
        Image bgImage = loadImage(GRASS_IMAGE);
        grassBackground = new TiledLayer(9, 1, bgImage, 32, 32);
        int numberOfTiles = width / 32 + 1;
        for(int i=0; i<numberOfTiles; i++) {
            grassBackground.setCell(i, 0, 1);
        }
        grassBackground.setPosition(0, 
                height - grassBackground.getHeight());
        layerManager.append(grassBackground);
    }
     * 
     */
    
    public void createMrSmith(){
        mrSmith = new Sprite(loadImage(MR_SMITH_IMAGE), 30, 30);
        int xPos = width/2;
        int yPos = height - mrSmith.getHeight() - 32;
        //int yPos = height - mrSmith.getHeight() -
        //        grassBackground.getHeight();
        mrSmith.setPosition(xPos, yPos);
        mrSmith.defineReferencePixel(15, 15);
        layerManager.append(mrSmith);
    }
    
    public void run() {
        while(gameIsOn) {
            getUserInput();
            /*
            if(rockIsThrown) {
                //moveRock();
            }            
             * 
             */
            try{
                Thread.sleep(speed);
            }catch(Exception e){
            }
            updateScreen();
        }
        //host.showMainScreen();
    }
    
    private void getUserInput(){
        int ks = getKeyStates();
        if((ks & RIGHT_PRESSED) != 0) {
            if(mrSmith.getX() < getWidth() -
                    mrSmith.getWidth()){
                mrSmith.move(3, 0);
                mrSmith.setTransform(Sprite.TRANS_NONE);
                mrSmith.nextFrame();
                direction = DIRECTION_RIGHT;
            }
        }
        if((ks & LEFT_PRESSED) != 0){
            if(mrSmith.getX() > 0) {
                mrSmith.setTransform(Sprite.TRANS_MIRROR);
                mrSmith.nextFrame();
                mrSmith.move(-3, 0);
                direction = DIRECTION_LEFT;
            }
        }
        /*
        if((ks & FIRE_PRESSED) != 0) {
            if(rockIsThrown == false) {
                rockIsThrown = true;
                rock.setPosition(mrSmith.getX() + rock.getWidth()/2, 
                        mrSmith.getY() + rock.getHeight()/2);
                //rock.set
            }
        }
         * 
         */
    }
    
    private void updateScreen(){
        Graphics g = getGraphics();
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, width, height);
        
        //g.setColor(0, 0, 0);
        //g.setFont(Font.get);
        
        layerManager.paint(g, 0, 0);
        flushGraphics();
    }
    
    public void start(){
        layerManager = new LayerManager();
        layerManager.setViewWindow(0, 0, width, height);
        //createBackground();
        createMrSmith();
        
        g = getGraphics();
        layerManager.paint(g, 0, 0);
        
        new Thread(this).start();
        //new Bird
        
        Thread runner = new Thread(this);
        runner.start();
    }
}
